package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.annotation.OperationLog;
import com.cskaoyan.catmall.model.bo.AdminCreateBo;
import com.cskaoyan.catmall.model.bo.AdminDeleteBo;
import com.cskaoyan.catmall.model.bo.AdminListBo;
import com.cskaoyan.catmall.model.bo.AdminUpdateBo;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.admin.AdminCreateVo;
import com.cskaoyan.catmall.model.vo.admin.AdminListVo;
import com.cskaoyan.catmall.model.vo.admin.AdminUpdateVo;
import com.cskaoyan.catmall.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 创建日期: 2022-01-08-22-54
 * author : WQW
 * description: 管理员相关的增删改查
 */

@RestController
@RequestMapping("admin/admin")
public class AdminController {

    @Autowired
    AdminService adminService;


    /**
     * @description 获取所有管理员信息
     * @author WQW
     * @date 2022/1/8 23:20
     * @param
     * @param adminListBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @OperationLog(value = "查询管理员")
    @GetMapping("list")
    public BaseRespVo list(@Validated AdminListBo adminListBo, BindingResult bindingResult){
       AdminListVo adminListVo = adminService.selectAdminList(adminListBo);
       return BaseRespVo.ok(adminListVo);
    }


    /**
     * @description
     * @author WQW
     * @date 2022/1/10 10:34
     * @param
     * @param adminCreateBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @OperationLog(value = "查询管理员")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated AdminCreateBo adminCreateBo,BindingResult bindingResult){
        AdminCreateVo adminCreateVo =adminService.addNewAdmin(adminCreateBo);
        if (adminCreateVo.getId()==null){
            return BaseRespVo.fail("新增管理员失败");
        }
        return BaseRespVo.ok(adminCreateVo);
    }


    /**
     * @description
     * @author WQW
     * @date 2022/1/10 10:34
     * @param
     * @param adminDeleteBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @OperationLog(value = "删除管理员")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody @Validated AdminDeleteBo adminDeleteBo,BindingResult bindingResult){
        int result = adminService.deleteAdmin(adminDeleteBo);
        if (result==200){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除管理员失败，请稍后重试");
    }

    /**
     * @description
     * @author WQW
     * @date 2022/1/10 10:34
     * @param
     * @param adminUpdateBo
     * @return com.cskaoyan.catmall.model.vo.BaseRespVo
     */
    @OperationLog("修改管理员")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated AdminUpdateBo adminUpdateBo,BindingResult bindingResult){
        AdminUpdateVo adminUpdateVo = adminService.updateAdmin(adminUpdateBo);
        if (adminUpdateVo==null){
            return BaseRespVo.fail("修改管理员信息失败，请稍后重试");
        }
        return BaseRespVo.ok(adminUpdateVo);
    }
}
